<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: html_output.php 426 2014-01-19 11:41:07Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 426 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-19 11:41:07 +0000 (Sun, 19 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright(c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

defined( '_VALID_XTC' ) or die( 'Direct Access to this location is not allowed.' );

function href_link($page = '', $parameters = '', $connection = 'NONSSL') {
	if($page == '') 
		die('<p>
				<span style="color:#b20000">
					<b>Fehler!</b>
				</span><br /><br />
				<b>Konnte den folgenden Link nicht zusammen setzen!<br /><br />
				genutze Funktion:<br /><br />
				href_link(\''.$page.'\', \''.$parameters.'\', \''.$connection.'\')</b>
			</p>');
	
	if($connection == 'NONSSL')
		$link = HTTP_SERVER.DIR_WS_ADMIN;
	elseif($connection == 'SSL') {
		if(ENABLE_SSL == 'true')
			$link = HTTPS_SERVER.DIR_WS_ADMIN;
		else
			$link = HTTP_SERVER.DIR_WS_ADMIN;
		
	} else {
		die('<p>
				<span style="color:#b20000">
					<b>Fehler!</b>
				</span><br /><br />
				<b>Konnte den folgenden Link nicht zusammen setzen!<br /><br />
				genutze Funktion:<br /><br />
				href_link(\''.$page.'\', \''.$parameters.'\', \''.$connection.'\')</b>
			</p>');
	}

	if(isset($_GET['menu']))
		$menu_id = 'menu='.$_GET['menu'];

	if(empty($parameters))
		$link = $link.$page.'?'.$menu_id.'&'. SID;
	elseif(strstr($parameters, 'menu='))
		$link = $link.$page.'?'.$parameters.'&'. SID;
	elseif(!strstr($parameters, 'menu='))
		$link = $link.$page.'?'.$menu_id.'&'. $parameters .'&'. SID;
	
	while((substr($link, -1) == '&') || (substr($link, -1) == '?'))
		$link = substr($link, 0, -1);

	return $link;
}

function catalog_href_link($page = '', $parameters = '', $connection = 'NONSSL') {
	if($connection == 'NONSSL')
		$link = HTTP_SERVER.DIR_WS_CATALOG;
	elseif($connection == 'SSL') {
		if(ENABLE_SSL == 'true')
			$link = HTTPS_SERVER.DIR_WS_CATALOG;
		else
			$link = HTTP_SERVER.DIR_WS_CATALOG;

	} else {
		die('<p>
				<span style="color:#b20000">
					<b>Fehler!</b>
				</span><br /><br />
				<b>Konnte den folgenden Link nicht zusammen setzen!<br /><br />
				genutze Funktion:<br /><br />
				href_link(\''.$page.'\', \''.$parameters.'\', \''.$connection.'\')</b>
			</p>');
	}
	
	if($parameters == '')
		$link .= $page;
	else
		$link .= $page.'?'.$parameters;
	
	while((substr($link, -1) == '&') || (substr($link, -1) == '?') )
		$link = substr($link, 0, -1);

	return $link;
}

function image($src, $alt = '', $width = '', $height = '', $params = '') {
	$image = '<img src="'.$src.'" border="0" alt="'.$alt.'"';
	
	if($alt) 
		$image .= ' title=" '.$alt.' "';
	
	if($width) 
		$image .= ' width="'.$width.'"';
	
	if($height) 
		$image .= ' height="'.$height.'"';
	
	if($params) 
		$image .= ' '.$params;
	
	$image .= '>';

	return $image;
}

function black_line() {
	return image(DIR_WS_IMAGES.'pixel_black.gif', '', '100%', '1');
}

function draw_separator($image = 'pixel_black.gif', $width = '100%', $height = '1') {
	return image(DIR_WS_IMAGES.$image, '', $width, $height);
}

function draw_form($name, $action, $parameters = '', $method = 'POST', $params = '') {
	$form = '<form name="'.$name.'" action="';

	if($parameters) {
		$form .= href_link($action, $parameters);
		$url .= href_link($action, $parameters);
		
	} else {
		$form .= href_link($action);
		$url = href_link($action);
	}
	
	$form .= '" method="'.$method.'"';
	
	if($params) 
		$form .= ' '.$params;
	
	$form .= '>';
	
	if((isset($_GET['menu']) || isset($_POST['menu'])) && strpos($url, 'menu=') === false)
		$form .= '<input type="hidden" name="menu" value="'.$_GET['menu'].$_POST['menu'].'" />'."\n";
		
	return $form;
}

function draw_input_field($name, $value = '', $parameters = '', $required = false, $type = 'text', $reinsert_value = true) {
	$field = '<input type="'.$type.'" name="'.$name.'"';
	
	if(($GLOBALS[$name]) && ($reinsert_value) )
		$field .= ' value="'.htmlspecialchars(trim($GLOBALS[$name])).'"';
	elseif($value != '')
		$field .= ' value="'.htmlspecialchars(trim($value)).'"';
	
	if($parameters != '')
		$field .= ' '.$parameters;
	
	$field .= '>';

	if($required) 
		$field .= TEXT_FIELD_REQUIRED;

	return $field;
}

function draw_small_input_field($name, $value = '', $parameters = '', $required = false, $type = 'text', $reinsert_value = true) {
	$field = '<input type="'.$type.'" size="3" name="'.$name.'"';
	
	if(($GLOBALS[$name]) && ($reinsert_value) )
		$field .= ' value="'.htmlspecialchars(trim($GLOBALS[$name])).'"';
	elseif($value != '')
		$field .= ' value="'.htmlspecialchars(trim($value)).'"';
	
	if($parameters != '')
		$field .= ' '.$parameters;
	
	$field .= '>';

	if($required) 
		$field .= TEXT_FIELD_REQUIRED;

	return $field;
}

function draw_password_field($name, $value = '', $required = false) {
	$field = draw_input_field($name, $value, 'maxlength="40"', $required, 'password', false);

	return $field;
}

function draw_file_field($name, $required = false) {
	$field = draw_input_field($name, '', '', $required, 'file');

	return $field;
}

function draw_selection_field($name, $type, $value = '', $checked = false, $compare = '') {
	$selection = '<input type="'.$type.'" name="'.$name.'"';

	if($value != '') 
		$selection .= ' value="'.$value.'"';

	if(($checked == true) || ($GLOBALS[$name] == 'on') || ($value && ($GLOBALS[$name] == $value)) || ($value && ($value == $compare)) ) 
		$selection .= ' checked=""';

	$selection .= '>';

	return $selection;
}

function draw_checkbox_field($name, $value = '', $checked = false, $compare = '') {
	return draw_selection_field($name, 'checkbox', $value, $checked, $compare);
}

function draw_radio_field($name, $value = '', $checked = false, $compare = '') {
	return draw_selection_field($name, 'radio', $value, $checked, $compare);
}

function draw_radio_field_jq($name, $vals, $checked = '', $desc = '') {
	$r = '<span id="'.$name.'">';
	foreach($vals AS $values) { $i++;
		$r .= '<input type="radio" name="'.$name.'" id="'.$name.'_'.$i.'" value="'.$values[0].'"'.($values[0] == $checked ? ' checked="checked"' : '').' /><label class="'.($values[0] == '1' ? 'ui-green-button' : 'ui-red-button').'" for="'.$name.'_'.$i.'">'.niceValue($values[1]).'</label>';
	}
	return $r.'</span><script type="text/javascript">$(function(){$("#'.$name.'").buttonset().fadeIn();})</script>';
}

function draw_textarea_field($name, $wrap, $width, $height, $text = '', $params = '', $reinsert_value = true) {
	$field = '<textarea id="'.$name.'" name="'.$name.'" wrap="'.$wrap.'" cols="'.$width.'" rows="'.$height.'"';
	
	if($params) $field .= ' '.$params;
		$field .= '>';
	
	if(($GLOBALS[$name]) && ($reinsert_value) ) 
	  $field .= $GLOBALS[$name];
	elseif($text != '') 
	  $field .= $text;
	
	$field .= '</textarea>';
	return $field;
}

function draw_hidden_field($name, $value = '') {
	$field = '<input type="hidden" name="'.$name.'" value="';
	
	if($value != '') 
		$field .= trim($value);
	else
		$field .= trim($GLOBALS[$name]);

	$field .= '" />';

	return $field;
}

function draw_pull_down_menu($name, $values, $default = '', $params = '', $required = false) {
	$field = '<select name="'.$name.'"';
	
	if($params) 
		$field .= ' '.$params;
	
	$field .= '>';

	if(is_array($values)){
		foreach($values as $key => $val) {
			$field .= '<option value="' .$val['id'].'"';
			if(((strlen($val['id']) > 0) && ($GLOBALS[$name] == $val['id'])) || ($default == $val['id']) ) 
				$field .= ' selected=""';
	
			$field .= '>'.$val['text'].'</option>';
		}
	}
	
	$field .= '</select>';

	if($required) 
		$field .= TEXT_FIELD_REQUIRED;

	return $field;
}

function sorting($page, $sort) {
	$nav  = '<div class="sort_arrow_placeholder"><span class="sort_arrows"><a title="'.IMAGE_ICON_DOWN.'" href="'.href_link($page,'sorting='.$sort.'&'.get_all_get_params(array('action','sorting'))).'">';
	$nav .= image(DIR_WS_ICONS.'sort_down.gif', '', '16' ,'16').'</a>';
	$nav .= '<a title="'.IMAGE_ICON_UP.'" href="'.href_link($page,'sorting='.$sort.'-desc&'.get_all_get_params(array('action','sorting'))).'">';
	$nav .= image(DIR_WS_ICONS.'sort_up.gif', '', '16' ,'16').'</a></span></div>';
	return $nav;
}

function deleteCacheFolders() {
	
	if(MEMCACHE_USE == 'true' && class_exists('Memcache')) {
		$memcache_obj = memcache_connect(MEMCACHE_HOSTS, MEMCACHE_PORT);
		memcache_flush($memcache_obj);
	}
	
	$del_array = array(array('templates_c/', 'Template Cache im BackEnd'),
						array('cache/', 'Cache im BackEnd'),
						array(DIR_FS_CATALOG.'cache/', 'Haupt-Cache Ordner im Shoproot'),
						array(DIR_FS_CATALOG.'templates_c/', 'Template Cache im Shoproot'));
	
	foreach($del_array AS $key => $value) {
		$dir = $value[0];
		$desc = $value[1];
	
		if(is_dir($dir)) {
			$error = false;
			$no_file = false;
		    if($dh = opendir($dir)) {
		        while(($file = readdir($dh)) !== false) {
		            if($file != '..' && $file != '.' && $file != 'index.html' && $file != '.htaccess' && $file != '.svn' && $file != '.git' && $file != 'cache_xajax.min.js') {
						if(is_dir($dir.$file))
							$f += rrmdir($dir.$file);
						elseif(@unlink($dir.$file))
							$i++;
						else {
							$error = true;
							$filename .= $dir.$file.' <b>konnte nicht gel&ouml;scht werden</b><br />';
						}
					}
					$files = $i;
		        }
		        closedir($dh);
		    }
		    if($error)
		    	$response .= $filename;
		    else {
		    	if($i > 0)
		 			$response .= '<br />Aus dem Verzeichnis <em>'.$dir.'</em> - '.$desc.' wurden <b>'.$i.'</b> Dateien gel&ouml;scht.<br />';
		 		else
		 			$response .= '<br />Das Verzeichnis <em>'.$dir.'</em> - '.$desc.' ist bereits leer.<br />';
		    }
		} else
			$response .= 'Verzeichnis <em>'.$dir.'</em> nicht gefunden';
		
	}
	if($f > 0)
		$response .= '<br />Aus dem Verzeichnis <em>/cache/</em> wurden <b>'.$f.'</b> Verzeichnisse gelöscht.';

	return'<div class="delete_response">'.$response.'</div>';
}

function rrmdir($dir) {
	if(is_dir($dir)) {
		$objects = scandir($dir);
		$i = 0;
		foreach($objects AS $object) {
			if($object != "." && $object != "..") {
				if(filetype($dir."/".$object) == "dir")
					rrmdir($dir."/".$object);
				else 
					@unlink($dir."/".$object);
				$i++;
			}
		}
		reset($objects);
		@rmdir($dir);
	}
	return $i;
}

function mouseOverJS($title='',$text,$img='',$width='') {
	if($text !='') {
		$fade_out = ', FADEOUT, 500';
		$padding = ', PADDING, 10';
		if($width =='')
			$w = ', WIDTH, 400';
		else
			$w = ', WIDTH, '.$width;
		$follow_mouse = ', FOLLOWMOUSE, false';
		if($title !='')
			$titel = ', TITLE, \''.$title.'\'';
		if($img !='')
			$text = '<img class=&quot;img_border&quot; src=&quot;'.$img.'&quot; alt=&quot;'.$title.'&quot; align=&quot;left&quot; style=&quot;margin: 0 10px 10px 0&quot; >'.$text;
		$over = ' onmouseover="Tip(\''.$text.'\''.$titel.''.$backg_color.''.$border_color.''.$fade_in.''.$fade_out.''.$padding.''.$color.''.$w.''.$follow_mouse.''.$shadow.''.$title_bg.''.$title_color.')" onmouseout="UnTip()"';
		return $over;
	}
	return;
}

function get_help($id = '', $title = '') {
	$titel = '';
	
	if(!empty($id)) {
		if(!empty($title))
			$titel = ' title="'.$title.'"';
		$help = '<a class="help_tip" style="vertical-align:middle;" href="javascript:void(0);" rel="includes/functions/get_faq_help.php?id='.$id.'"'.$titel.'><img src="'.DIR_WS_ICONS.'question-frame.png'.'" style="position:relative;top:3px" alt="FAQ" /></a>';
		return $help;
	}
}

function datei_vorhanden($file) {
	if(file_exists($file))
		return true;
	else
		return false;
}

function getMenuID($name, $gID = '') {
	global $db;
	
	if(!empty($gID))
		$and = " AND group_id = '".(int)$gID."' ";

	$id = $db->db_query("SELECT
								parent_id
							FROM
								".TABLE_ADMIN_MENU."
							WHERE
								menu_name = '".$name."'".$and);
	if($id->_numOfRows)
		return 'menu='.$id->fields['parent_id'];
}